var RoundRectangle = {
	wrap: function(e, r, w, h, b){
		if(b == null)			b = '';
		if(w == null)			w = '100%';
		if(h == null)			h = '100%';
		if(r == null || r<=0)	r = 1;
		var s = '', si, sj, id = e.id+'_rr_', i, j, k, p = [[],[],[]], d = ' width=1 ', c = ' height=1 ',
			i1= b.match(/T/i)?(0):(-r), i2=b.match(/B/i)?(0):(r), j1=b.match(/L/i)?(0):(-r), j2=b.match(/R/i)?(0):(r);
		e.margin = r;
		e.bordersOptions = b;
		e.cells = [];
		e.getCell = function(i,j){return $(this.id+'_rr_'+((i<0)?('n'+(-i)):(''+i))+'_'+((j<0)?('n'+(-j)):(''+j)));}
		e.resetRoundRectangleColor = function(bgColor, borderColor, outColor){
			this.cells.each(function(x){$(x).style.backgroundColor = {outside:outColor, onborder:borderColor, inside:bgColor}[$(x).positionType];});
		}
		for(i=i1; i<=i2; i++){
			si = id+((i<0)?('n'+(-i)):(''+i));
			s += '<tr'+(i==0?'':c)+' id="'+si+'">';
			if(i==i1 && b.match(/L/)){
				s += '<td id="'+id+'L" rowspan='+(r+r+1)+d+'></td>';
				p[1].push(id+'L');
			}
			for(j=j1; j<=j2; j++){
				sj = si+'_'+((j<0)?('n'+(-j)):(''+j));
				k = i*i+j*j-r*r-0.25;
				p[(k>r)?0:((k>-r)?1:2)].push(sj);
				s += '<td'+(j==0?'':d)+' id="'+sj+'">';
				if(i == 0 && j == 0)
					s += '<div id="'+e.id+'_">'+e.innerHTML+'</div>';
				s += '</td>';
			}
			if(i==i1 && b.match(/R/)){
				s += '<td id="'+id+'R" rowspan='+(r+r+1)+d+'></td>';
				p[1].push(id+'R');
			}
			s += '</tr>';
		}
		if(b.match(/T/)){
			s = '<tr'+c+'><td id="'+id+'T" colspan='+(r+r+1)+'></td></tr>' + s;
			p[1].push(id+'T');
		}
		if(b.match(/B/)){
			s = s + '<tr'+c+'><td id="'+id+'B" colspan='+(r+r+1)+'></td></tr>';
			p[1].push(id+'B');
		}
		e.innerHTML = '<table width='+w+' height='+h+' id="'+id+'tb" cellpadding=0 cellspacing=0 border=0>'+s+'</table>';
		for(i=0; i<3; i++)
			p[i].each(function(x){e.cells.push($(x));$(x).positionType=['outside', 'onborder', 'inside'][i];});
		e.table = $(id+'tb');
		e.leftCell = $(id+'L');
		e.rightCell = $(id+'R');
		e.topCell = $(id+'T');
		e.bottomCell = $(id+'B');
	},

	unwrap: function(e){
		var a = $(e.id+'_');
		if(a == null)
			return;
		e.margin = undefined;
		e.bordersOptions = undefined;
		e.cells = undefined;
		e.getCell = undefined;
		e.resetRoundRectangleColor = undefined;
		e.table = undefined;
		e.leftCell = undefined;
		e.rightCell = undefined;
		e.topCell = undefined;
		e.bottomCell = undefined;
		e.innerHTML = a.innerHTML;
	}
};

var TabController = Class.create();

TabController.prototype = {
	initialize: function(tabs,pages,unselectTab,selectTab,unselectPage,selectPage,selectedIndex){
		if(tabs == null || !(tabs instanceof Array))
			tabs = [];
		if(pages == null || !(pages instanceof Array) || pages.length == 0){
			unselectPage = function(){};
			selectPage = function(){};
			pages = new Array(tabs.length);
		}
		else
			tabs.splice(pages.length);
		if(selectedIndex == null)
			selectedIndex = (tabs.length>0)?0:-1;
		for(var i=0;i<tabs.length;i++){
			tabs[i] = $(tabs[i]);
			tabs[i].onclick = function(){this.tabControl.select(this.tabIndex);};
			tabs[i].tabControl = this;
			tabs[i].tabIndex = i;
			pages[i] = $(pages[i]);
		}
		this.tabs = tabs;
		this.pages = pages;
		this.unselectTab = unselectTab;
		this.selectTab = selectTab;
		this.unselectPage = unselectPage;
		this.selectPage = selectPage;
		this.selectedIndex = selectedIndex;

		tabs.each(unselectTab);
		pages.each(unselectPage);
		if(selectedIndex>=0){
			selectTab(tabs[selectedIndex]);
			selectPage(pages[selectedIndex]);
		}
	},

	select: function(tab){
		if(typeof(tab) != 'number'){
			tab = $(tab);
			for(var i = this.tabs.length-1;i>=0 && this.tabs[i] != tab;i--);
			tab = i;
		}
		if(this.selectedIndex>=0){
			this.unselectTab(this.tabs[this.selectedIndex]);
			this.unselectPage(this.pages[this.selectedIndex]);
		}
		this.selectedIndex = tab;
		if(tab>=0){
			this.selectTab(this.tabs[tab]);
			this.selectPage(this.pages[tab]);
		}
	}
}
